package vn.edu.uit.chemistry;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Map;

import vn.edu.uit.chemistry.utilities.TCPClient;
import vn.edu.uit.chemistry.view.ViewGuiTaiLieu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GuiTaiLieuBB extends Activity {

	TextView tvTitle;
	TextView tvFile;
	TextView tvSize;
	
	ImageView imgGuiFile;
	Button btnBack;
	
	String path = "";
	String title = "";
	
	public static LinearLayout lvGuiTL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_gui_tai_lieu);

		Intent startIntent = getIntent();
		if (startIntent != null) {
			Bundle b = startIntent.getExtras();
			path = b.getString("File");
			title = b.getString("Title");
		}
		
		File myfile = new File(path);
		
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		if (title.equals("BCTK")) {
			tvTitle.setText("Gửi bài cũ tham khảo");
		} else if (title.equals("TL")) {
			tvTitle.setText("Gửi tài liệu cho bạn bè");
		} else if (title.equals("TLBK")) {
			tvTitle.setText("Gửi tài liệu");
		}
		
		imgGuiFile = (ImageView) findViewById(R.id.imgNhanFile);
		tvFile = (TextView) findViewById(R.id.tvFile);
		tvFile.setText(myfile.getName());
		tvSize = (TextView) findViewById(R.id.tvSize);
		tvSize.setText("" + myfile.length() + " bytes");
		
		lvGuiTL = (LinearLayout) findViewById(R.id.listGuiFile);
		imgGuiFile.postDelayed(onEverySecond, 1);
		
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//new SendAsyncTask().execute();
		guiChoBanBe();
	}
	
	public void guiChoBanBe() {
		SharedPreferences prefs = getSharedPreferences("DSGui", 0);
		Map<String, ?> keys = prefs.getAll();
		for (Map.Entry<String, ?> entry : keys.entrySet()) {
			Log.d("DSGui", entry.getKey() + "#"
					+ entry.getValue().toString());
			SharedPreferences.Editor editor = prefs.edit();
			String value = prefs.getString(entry.getKey(), "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			editor.putString(entry.getKey(), value + 0);
			editor.commit();
			
			new SendAsyncTask(entry.getKey()).execute();
		}
	}
	
	final class SendAsyncTask extends AsyncTask<Void, Void, Void> {
		
		Context ctx;
		String key = "";

		public SendAsyncTask(String _key) {
			this.ctx = GuiTaiLieuBB.this;
			this.key = _key;
		}

		@Override
		protected Void doInBackground(Void... params) {
			
				try {
					
					if (title.equals("BCTK")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiBaiCuTK(path);
					} else if (title.equals("TL")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiTaiLieuChoBB(path);						
					} else if (title.equals("TLBK")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiFileBatKyChoBB(path);
					}
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			
			return null;
		}

	}
	
	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new UpdatePrecentAsyncTask(getApplicationContext()).execute();
		}
	};
	
	final class UpdatePrecentAsyncTask extends AsyncTask<Void, Void, Void> {

		private Context context;
		private ViewGuiTaiLieu viewGuiTaiLieu;
		private LayoutParams lp;
		private Map<String, ?> keys;

		public UpdatePrecentAsyncTask(Context ctx) {
			this.context = ctx;
		}

		@Override
		protected Void doInBackground(Void... param) {
			SharedPreferences prefs = context.getSharedPreferences("DSGui", 0);
			keys = prefs.getAll();

			return null;
		}

		@Override
		protected void onPostExecute(final Void unused) {
			lvGuiTL.removeAllViews();
			for (Map.Entry<String, ?> entry : keys.entrySet()) {
				String[] values = entry.getValue().toString().split("#");
				viewGuiTaiLieu = new ViewGuiTaiLieu(context);
				viewGuiTaiLieu.setContent(values[1], values[0],
						Integer.parseInt(values[2]));
				lvGuiTL.addView(viewGuiTaiLieu.layout);
			}

			imgGuiFile.postDelayed(onEverySecond, 1);
		}

	}
}
